<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>崩坏·星穹铁道游戏助手</title>
    <style>
        html,
        body {
            margin: 0;
            padding: 0;
            height: 100vh;
            width: 100vw;
            /* 移除 html 和 body 默认的边框 */
            border: none;
        }

        body {
            font-family: Arial, sans-serif;
            max-width: none;
            padding: 20px;
            box-sizing: border-box;
            position: relative;
            z-index: 1;
            display: flex;
            gap: 20px;
        }

        body::before {
            content: "";
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-image: url('https://upload-bbs.miyoushe.com/upload/2025/02/21/296292055/a2707f0ad1813cc1092b7bc47061bec4_1873266202704600867.jpg?x-oss-process=image//resize,s_600/quality,q_80/auto-orient,0/interlace,1/format,jpg');
            background-size: cover;
            background-position: center;
            opacity: 0.5;
            z-index: -1;
            /* 移除伪元素的边框 */
            border: none;
        }

        #history-container {
            flex: 1;
            background-color: transparent;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
            padding: 20px;
            max-height: 100%;
            overflow-y: auto;
        }

        #chat-container {
            flex: 2;
            background-image: url('your-custom-chat-background.jpg');
            background-size: cover;
            background-position: center;
            background-repeat: no-repeat;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
            padding: 20px;
            overflow: hidden;
            max-height: 100%;
            display: flex;
            flex-direction: column;
        }

        #chat-history {
            flex-grow: 1;
            overflow-y: auto;
            border-radius: 4px;
            padding: 10px;
            margin-bottom: 10px;
        }

        .message {
            margin-bottom: 10px;
            padding: 8px;
            border-radius: 12px;
            max-width: 40%;
            word-wrap: break-word;
            display: flex;
            align-items: center;
            position: relative;
        }

        .user-message {
            background-color: #e3f2fd;
            margin-left: 50%;
            flex-direction: row-reverse;
        }

        .assistant-message {
            background-color: #f1f8e9;
            margin-right: 30%;
        }

        .avatar {
            width: 40px;
            height: 40px;
            border-radius: 50%;
            object-fit: cover;
            position: absolute;
            top: 50%;
            transform: translateY(-50%);
            /* 移除头像的边框 */
            border: none;
        }

        .user-message .avatar {
            right: -50px;
        }

        .assistant-message .avatar {
            left: -50px;
        }

        .message-content {
            flex-grow: 1;
        }

        /* 修改输入框和发送按钮的父容器样式 */
        #chat-container>:last-child {
            display: flex;
            gap: 10px;
        }

        #user-input {
             /* 减小 flex-grow 值，让输入框宽度变窄 */
             flex-grow: 0.25; 
            /* 增大字体大小 */
            font-size: 20px; 
            padding: 4px;
            border-radius: 4px;
            background-color: rgba(255, 255, 255, 0.5);
            /* 让宽度随内容自由浮动 */
            width: auto; 
            /* 设置最小宽度，避免过窄 */
            min-width: 100px; 
        }

        #send-btn {
            padding: 8px 16px;
            background-color: rgba(255, 192, 203, 0.5);
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            white-space: nowrap;
        }

        #send-btn:hover {
            background-color: rgba(255, 160, 122, 0.7);
        }

        .history-item {
            margin-bottom: 10px;
            padding: 8px;
            border-radius: 4px;
            cursor: pointer;
        }

        /* 修改鼠标悬停背景颜色为透明橙色 */
        .history-item:hover {
            background-color: rgba(255, 165, 0, 0.3);
        }
    </style>
</head>

<body>
    <div id="history-container">
        <h2>历史搜索记录</h2>
        <div id="history-list"></div>
    </div>
    <div id="chat-container">
        <h1>崩坏·星穹铁道游戏助手</h1>
        <div id="chat-history"></div>
        <input type="text" id="user-input" placeholder="请输入您的问题">
        <button id="send-btn">发送</button>
    </div>

    <script>
        const chatHistory = document.getElementById('chat-history');
        const userInput = document.getElementById('user-input');
        const sendBtn = document.getElementById('send-btn');
        const historyList = document.getElementById('history-list');

        let history = [
            {
                "role": "system",
                "content": "你是一个《崩坏·星穹铁道》的游戏助手，你能给每个用户给出的角色提供最优配队，你的配对总是正确的."
            },
            {
                "role": "user",
                "content": "请用中文进行自我介绍，要求不能超过5句话，总字数不超过100个字。"
            }
        ];

        const userAvatar = './OIP-.jpg';
        const assistantAvatar = './startIcon.png';

        function displayMessage(role, content) {
            const messageDiv = document.createElement('div');
            messageDiv.className = `message ${role}-message`;

            const avatarImg = document.createElement('img');
            avatarImg.className = 'avatar';
            avatarImg.src = role === 'user' ? userAvatar : assistantAvatar;
            avatarImg.alt = `${role} avatar`;

            avatarImg.onerror = () => {
                console.error(`Failed to load ${role} avatar: ${avatarImg.src}`);
            };

            const textDiv = document.createElement('div');
            textDiv.className = 'message-content';
            textDiv.textContent = content;

            messageDiv.appendChild(avatarImg);
            messageDiv.appendChild(textDiv);

            chatHistory.appendChild(messageDiv);
            chatHistory.scrollTop = chatHistory.scrollHeight;
        }

        function displayHistory() {
            historyList.innerHTML = '';
            const userHistory = history.filter(item => item.role === 'user');
            userHistory.forEach(item => {
                const historyItem = document.createElement('div');
                historyItem.className = 'history-item';
                historyItem.textContent = item.content;
                historyItem.addEventListener('click', () => {
                    userInput.value = item.content;
                });
                historyList.appendChild(historyItem);
            });
        }

        async function sendMessage() {
            const userMessage = userInput.value.trim();
            if (!userMessage) return;

            displayMessage('user', userMessage);
            userInput.value = '';

            try {
                const response = await fetch('http://127.0.0.1:5000/chat', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({
                        history: history,
                        user_message: userMessage
                    })
                });

                if (!response.ok) {
                    throw new Error('请求失败');
                }

                const data = await response.json();
                if (data.error) {
                    throw new Error(data.error);
                }

                displayMessage('assistant', data.assistant_reply);
                history = data.updated_history;
                displayHistory();
            } catch (error) {
                console.error('错误:', error);
                displayMessage('assistant', '抱歉，处理请求时出现错误。');
            }
        }

        sendBtn.addEventListener('click', sendMessage);
        userInput.addEventListener('keypress', (e) => {
            if (e.key === 'Enter') {
                sendMessage();
            }
        });

        window.onload = () => {
            sendMessage();
            displayHistory();
        };
    </script>
</body>

</html>